package tiled.gwt.server;

import java.util.HashMap;
import java.util.Iterator;
import tiled.core.MapLayer;
import tiled.core.MapObject;
import tiled.core.ObjectGroup;
import tiled.core.Tile;
import tiled.core.TileLayer;
import tiled.core.TileSet;


public class TMXMapReader {
	
	public tiled.gwt.Map readMap(String fileToOpen){
		tiled.core.Map map=null;
		tiled.gwt.Map m = new tiled.gwt.Map();
        try {
        	tiled.io.TMXMapReader mapReader = new tiled.io.TMXMapReader();
     		map = mapReader.readMap(fileToOpen);
     		
     		m.orientation=map.getOrientation();
     		m.width=map.getWidth();
     		m.height=map.getHeight();
     		m.tilewidth=map.getTileWidth();
     		m.tileheight=map.getTileHeight();
     		
     		HashMap<Integer,Integer> tileSetsGids = new HashMap<Integer,Integer>();
     		
     		int i=0;
     		for (TileSet tileSet : map.getTileSets()) { //Serialize tilesets first!, than layers
            	tiled.gwt.TileSet ts=new tiled.gwt.TileSet();
           		ts.name=tileSet.getName();
           		ts.source=tileSet.getTilebmpFileName();
            	ts.tilesPerRow=tileSet.getTilesPerRow();
            	ts.tileWidth=tileSet.getTileWidth();
            	ts.tileHeight=tileSet.getTileHeight();
            	ts.spacing=tileSet.getTileSpacing();
            	ts.margin=tileSet.getTileMargin();
            	m.tileSets.put(i, ts);
            	tileSetsGids.put(tileSet.getFirstGid(),i);
            	i++;
            }
     		
            for (MapLayer layer : map) {
            	
                if (layer instanceof ObjectGroup) {
                	ObjectGroup objectGroup = (ObjectGroup) layer;
                	tiled.gwt.ObjectGroup og = new tiled.gwt.ObjectGroup();
                	
                	Iterator<MapObject> itr=objectGroup.getObjects();
                	while (itr.hasNext()) {
                		MapObject mapObject=itr.next();
                		
                		tiled.gwt.MapObject mo=new tiled.gwt.MapObject();
                		
                		mo.name=mapObject.getName();
                		mo.type=mapObject.getType();
                		mo.x=mapObject.getX();
                		mo.y=mapObject.getY();
                		mo.width=mapObject.getWidth();
                		mo.height=mapObject.getHeight();

                		og.objects.add(mo);
                	}
                	m.objectGroups.put(objectGroup.getName(), og);
                }

                if (!(layer instanceof ObjectGroup)) { // I can't specify "instanceof MapLayer" - it's not work, fucking shit ;/ 
                	tiled.gwt.MapLayer ml=new tiled.gwt.MapLayer();
                	//
                	TileLayer tileLayer = (TileLayer) layer;
                	
                	ml.tiles=new tiled.gwt.Tile[layer.getWidth()][layer.getHeight()];
                	
                	for(int y=0;y<layer.getHeight();y++){
                    	for(int x=0;x<layer.getWidth();x++){
                    		tiled.gwt.Tile t = new tiled.gwt.Tile(); 

                    		Tile tile=tileLayer.getTileAt(x, y);
                    		
                    		t.Tn=tileSetsGids.get(tile.getGid()-tile.getId());
                    		t.n=tile.getId();
                    		ml.tiles[x][y]=t;
                    	}
                	}
                	ml.width=layer.getWidth();
                	ml.height=layer.getHeight();
                	
                	m.mapLayers.put(layer.getName(),ml);
                }
                
            }
        } catch (Exception e) {
        	System.out.println(e.getMessage());
        }
		return m;
	}
	
}
